package com.eas.ereceipt.util;


import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.util.Log;

public class Util {


	public static boolean isDeviceSupportCamera(Context c) {
		if (c.getPackageManager().hasSystemFeature(
				PackageManager.FEATURE_CAMERA)) {
			// this device has a camera
			return true;
		} else {
			// no camera on this device
			return false;
		}
	}
	
	public static Bitmap decodeFile(String path){
		Bitmap b = null;
        //Decode image size
    BitmapFactory.Options o = new BitmapFactory.Options();
    o.inJustDecodeBounds = true;

    try{
    	//String name = new File(path).getName();
    	Log.e("tmpFile", path);
    	 FileInputStream fis = new FileInputStream(path);
    	    BitmapFactory.decodeStream(fis, null, o);
    	    fis.close();

    	    int scale = 1;
    	    if (o.outHeight > AppConfig.IMAGE_MAX_SIZE || o.outWidth > AppConfig.IMAGE_MAX_SIZE) {
    	        scale = (int)Math.pow(2, (int) Math.round(Math.log(AppConfig.IMAGE_MAX_SIZE / 
    	           (double) Math.max(o.outHeight, o.outWidth)) / Math.log(0.5)));
    	    }

    	    //Decode with inSampleSize
    	    BitmapFactory.Options o2 = new BitmapFactory.Options();
    	    o2.inSampleSize = scale;
    	    fis = new FileInputStream(path);
    	    b = BitmapFactory.decodeStream(fis, null, o2);
    	    fis.close();
    } catch (FileNotFoundException e) {
		e.printStackTrace();
	} catch (IOException e) {
		e.printStackTrace();
	}
   

    return b;
	}
	
	public static Bitmap decodeStream(Context c, Uri src){
		Bitmap b = null;
		InputStream i = null;
        //Decode image size
    BitmapFactory.Options o = new BitmapFactory.Options();
    o.inJustDecodeBounds = true;

    try{
        i = c.getContentResolver().openInputStream(src); 
    	BitmapFactory.decodeStream(i, null, o);
    	i.close();
    	    

    	    int scale = 1;
    	    if (o.outHeight > AppConfig.IMAGE_MAX_SIZE || o.outWidth > AppConfig.IMAGE_MAX_SIZE) {
    	        scale = (int)Math.pow(2, (int) Math.round(Math.log(AppConfig.IMAGE_MAX_SIZE / 
    	           (double) Math.max(o.outHeight, o.outWidth)) / Math.log(0.5)));
    	    }

    	    //Decode with inSampleSize
    	    BitmapFactory.Options o2 = new BitmapFactory.Options();
    	    o2.inSampleSize = scale;
    	    i = c.getContentResolver().openInputStream(src);
    	    b = BitmapFactory.decodeStream(i, null, o2);
    	    i.close();
    } catch (FileNotFoundException e) {
		e.printStackTrace();
	} catch (IOException e) {
		e.printStackTrace();
	}
   

    return b;
	}

}
